package com.woniu.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniu.pojo.Advancenotice;
import com.woniu.service.AdvancenoticeService;

@Controller
@RequestMapping("/advanceNotice")
public class AdvanceNoticeController {
	@Autowired
	public AdvancenoticeService advancenoticeService;
	@RequestMapping("/selectAdvancenoticeByhId")
	@ResponseBody
	public Advancenotice selectAdvancenoticeByhId(String house_id,HttpServletRequest request) {
		Advancenotice advancenotice = advancenoticeService.selectAdvancenoticeByhId(house_id);
		request.setAttribute("h_id", house_id);
		return advancenotice;
	}
	@RequestMapping("/addAdvancenotice")
	@ResponseBody
	public String addAdvancenotice(Advancenotice advancenotice) {
		int rowCount=0;
		if(advancenotice.getAdvanceNotice_id().equals("")) {
			rowCount = advancenoticeService.addAdvancenotice(advancenotice);
		}else {
			
			Advancenotice oldAdvancenotice = advancenoticeService.selectAdvancenoticeByhId(String.valueOf(advancenotice.getH_id()));
			advancenotice.setAdvanceNotice_name(oldAdvancenotice.getAdvanceNotice_name()+","+advancenotice.getAdvanceNotice_name());
			advancenotice.setAdvanceNotice_IDCard(oldAdvancenotice.getAdvanceNotice_IDCard()+","+advancenotice.getAdvanceNotice_IDCard());
			advancenotice.setAdvanceNotice_phone(oldAdvancenotice.getAdvanceNotice_phone()+","+advancenotice.getAdvanceNotice_phone());
			rowCount = advancenoticeService.updateAdvancenotice(advancenotice);
		}
		if(rowCount>0) {
			return "新增成功";
		}else {
			return "新增失败";
		}
	}
	@RequestMapping("/delAdvancenotice")
	@ResponseBody
	public String delAdvancenotice(String adName,String adID,String adPhone,String hId) {
		Advancenotice advancenotice = advancenoticeService.selectAdvancenoticeByhId(hId);
		String[] adNames= advancenotice.getAdvanceNotice_name().split(",");
		String[] adIDs=advancenotice.getAdvanceNotice_IDCard().split(",");
		String[] adPhones=advancenotice.getAdvanceNotice_phone().split(",");
		for(int i=0;i<adNames.length;i++) {
			if(adNames[i].equals(adName)) {
				adNames[i]=null;
				adIDs[i]=null;
				adPhones[i]=null;
				break;
			}
		}
		String newName="";
		String newId="";
		String newPhone="";
		for(int i=0;i<adNames.length;i++) {
			if(adNames[i]!=null) {
				if(i+1<adNames.length&&adNames[i+1]!=null) {
					newName=newName+adNames[i]+",";
					newId=newId+adIDs[i]+",";
					newPhone=newPhone+adPhones[i]+",";
				}else {
					newName=newName+adNames[i];
					newId=newId+adIDs[i];
					newPhone=newPhone+adPhones[i];
				}
			}
		}
		advancenotice.setAdvanceNotice_name(newName);
		advancenotice.setAdvanceNotice_IDCard(newId);
		advancenotice.setAdvanceNotice_phone(newPhone);
		System.out.println("***********"+newName);
		int rowCount = advancenoticeService.updateAdvancenotice(advancenotice);
		if(rowCount>0) {
			return "删除成功";
		}else {
			return "删除失败";
		}
	}
}
